package cn.kgc.springboot.vue.service.impl;

import cn.kgc.springboot.vue.entity.RolePermission;
import cn.kgc.springboot.vue.mapper.RolePermissionMapper;
import cn.kgc.springboot.vue.response.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.springboot.vue.entity.Permission;
import cn.kgc.springboot.vue.service.PermissionService;
import cn.kgc.springboot.vue.mapper.PermissionMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author mengshun
* @description 针对表【sys_permission】的数据库操作Service实现
* @createDate 2024-12-23 15:01:57
*/
@Service
@Transactional
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission>implements PermissionService{

    @Resource
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public ResponseResult deletePermission(Integer id) {

        // 1. 判定权限是否含有子权限
        LambdaQueryWrapper<Permission> lambda = new QueryWrapper<Permission>().lambda();
        lambda.eq(Permission::getPid,id);
        List<Permission> permissions = baseMapper.selectList(lambda);
        if (ObjectUtils.isNotNull(permissions)){
            return  ResponseResult.fail().message("含有子权限,禁止删除");
        }

        //  2. 删除权限
        baseMapper.deleteById(id);

        // 3. 删除权限和角色的关联数据
        LambdaQueryWrapper<RolePermission> lambda1 = new QueryWrapper<RolePermission>().lambda();
        lambda1.eq(RolePermission::getPerId,id);
        rolePermissionMapper.delete(lambda1);


        return ResponseResult.success();
    }
}




